//
//  UpdateAlipayAccountViewController.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/11/15.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "UpdateAlipayAccountViewController.h"
#import "BLHeader.h"
#import "UpdateNet.h"
#import <MBProgressHUD.h>

@interface UpdateAlipayAccountViewController ()
@property (weak, nonatomic) IBOutlet UITextField *alipay;
@property (weak, nonatomic) IBOutlet UITextField *trueName;

@end

@implementation UpdateAlipayAccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    self.title = @"添加支付宝账号";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(commitUpdate)];
    [right setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:kAppThemeColor ,NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = right;
}

- (void)commitUpdate {
    NSString *alipayAccount = self.alipay.text;
    NSString *name = self.trueName.text;
    
    if(!alipayAccount || alipayAccount.length == 0) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = @"请输入支付宝账号！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    
    if(!name || name.length == 0) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = @"请输入支付宝登记的实名！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    
    __weak typeof (self) weakSelf = self;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定账号正确？后期无法修改！" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction: [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [alert addAction: [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeIndeterminate;
        NSString *tel = [[NSUserDefaults standardUserDefaults] objectForKey:USERTEL];
        NSDictionary *param = @{@"userNum":alipayAccount, @"userTel": tel, @"payName" : name};
        [UpdateNet updateUserInfo:param completer:^(id resultObject, NSError *error) {
            if(resultObject) {
                hud.mode = MBProgressHUDModeText;
                hud.label.text = @"修改成功！";
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                    [hud hideAnimated:YES];
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                });
                return ;
            }
            [hud hideAnimated:YES];
        }];
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}
@end
